package com.example.androidproject.wallpaper.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.androidproject.app.ClassifyActivity3;
import com.example.androidproject.wallpaper.bean.WallpaperBean;
import com.example.androidproject.R;

import java.util.List;

public class WallpaperAdapter extends RecyclerView.Adapter {
    private final List<WallpaperBean.DataBean> contentList;
    private final Context mContext;
    private final LayoutInflater layoutInflater;
    private final int ITEM_BTN = 0;
    private final int ITEM_CONTENT = 1;
    private ItemOnClickListener itemOnClickListener;

    public WallpaperAdapter(Context mContext, List<WallpaperBean.DataBean> contentList) {
        this.mContext = mContext;
        this.contentList = contentList;
        layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type) {
                        case ITEM_BTN:
                            return 3;
                        case ITEM_CONTENT:
                            return 1;
                        default:
                            return 1;
                    }
                }
            });
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_BTN) {
            return new BTNViewHolder(layoutInflater.inflate(R.layout.wallpaper_index_btn, parent, false));
        } else
            return new ContentViewHolder(layoutInflater.inflate(R.layout.wallpaper_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof BTNViewHolder) {
            BTNViewHolder btnViewHolder = (BTNViewHolder) holder;
            btnViewHolder.setData();
        }
        if (holder instanceof ContentViewHolder) {
            ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
            contentViewHolder.setData(position - 1);
        }
    }

    @Override
    public int getItemCount() {
        if (contentList == null) return 0;
        return contentList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_BTN;
        } else {
            return ITEM_CONTENT;
        }
    }

    class BTNViewHolder extends RecyclerView.ViewHolder {
        public Button btn_classify, btn_collect;

        public BTNViewHolder(@NonNull View itemView) {
            super(itemView);
            btn_classify = itemView.findViewById(R.id.btn_classify);
            btn_collect = itemView.findViewById(R.id.btn_collect);
        }

        public void setData() {
            btn_classify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ClassifyActivity3.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("flag", 3);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });
        }
    }

    class ContentViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv_wallpaper_content;

        public ContentViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_wallpaper_content = itemView.findViewById(R.id.iv_wallpaper_content);
        }

        public void setData(int position) {
            Glide.with(mContext).load(contentList.get(position).getSrc()).into(iv_wallpaper_content);

            iv_wallpaper_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemOnClickListener.onClick(position);
                }
            });
        }
    }

    public void refresh() {
        notifyItemRangeChanged(1, contentList.size() + 1);
    }

    public interface ItemOnClickListener {
        void onClick(int position);
    }

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }
}
